<?php
/*
 * 老人信息展示接口
 */
class Apiv1_SeniorinfoController extends Ctrl_Apiatk{
    public function indexAction(){
        $p = $_REQUEST;
        $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);

        $tSMO = new SeniorModel;
        $tRIMO = new ReligioninfoModel;
        $tNIMO = new NationinfoModel;
        $tFMMO = new FamilymembersModel;
        $tSIMO = new SexinfoModel;
        $tWMO = new WorkerModel;
        $tAMO = new AdminModel;
        $tPMO = new PlanModel;
        $tWSMO = new WorkerseniorModel;

        $tImgUrl = Yaf_Registry::get("config")->web->url->img;

        //老人基本信息
        $tDatas = array(array());
        $tSql = 'select concat(\''.$tImgUrl.'\',s.photo) photo,s.goods goods,s.medical_orders medical_orders,s.name name,s.age age,ni.introduce nation,ri.introduce religion,fm.name fm_name,fm.relation fm_relation,fm.phone fm_phone from '.$tSMO->table.' s left join '.$tNIMO->table.' ni on s.nation = ni.num left join '.$tRIMO->table.' ri on s.religion = ri.num left join '.$tFMMO->table.' fm on s.id = fm.sid where s.id = '.$pSid.' and fm.is_client = 1';
        $tSData = $tSMO->query($tSql)[0];        
        $tDatas['senior'] = empty($tSData)?array():$tSData;
        //护工信息
        $tSql = 'select concat(\''.$tImgUrl.'\',w.photo) photo,w.name w_name,si.introduce w_sex,w.age w_age,w.native_place native_place,w.phone w_phone,w.healthy_status healthy_status,w.skill w_skill from '.$tAMO->table.' a left join '.$tWMO->table.' w on a.w_id = w.id left join '.$tWSMO->table.' ws on a.id = ws.sw_id left join '.$tSIMO->table.' si on w.sex = si.num where ws.sid = '.$pSid;
        $tWData = $tWSMO->query($tSql)[0];
        if(!empty($tWData)){
            foreach($tWData as $tRow){
                switch($tRow['healthy_status']){
                    case '1':$tWData['healthy_status'] = '优秀';break;
                    case '2':$tWData['healthy_status'] = '良好';break;
                    case '3':$tWData['healthy_status'] = '合格';break;
                    case '4':$tWData['healthy_status'] = '不合格';break;
                }
            }
        }
        $tDatas['worker'] = empty($tWData)?array():$tWData;
        //展示信息
        //$tPData = $tPMO->field('GROUP_CONCAT(event_type) event_type')->where('sid = '.$pSid.' and is_show = 1')->fList()[0];
        $tSql = 'select GROUP_CONCAT(event_type) event_type from '.$tPMO->table.' where sid = '.$pSid.' and is_show = 1';
        $tPData = $tPMO->query($tSql)[0];
        $tDatas['plan']['event_type'] = empty($tPData)?'':implode(',',array_unique(explode(',',$tPData['event_type'])));
        unset($tDatas[0]);
        Tool_Fnc::ajaxMsg('',1,$tDatas);
    }
    public function listAction(){
        $p = $_REQUEST;
        $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pDate = empty($p['date'])?'':Tool_Fnc::safe_string($p['date']);
        $pEventtype = empty($p['event_type'])?'':Tool_Fnc::safe_string($p['event_type']);
        //$pType = empty($p['type'])?'':Tool_Fnc::safe_string($p['type']);
        $tTime = time();

        $tWhere = ' 1 and date = \''.date('Y-m-d',$tTime).'\'';
        if(!empty($pDate)){
            $tWhere = ' 1 and date = \''.$pDate.'\'';
        }
        $tDatas = array(array());
        #血压信息
        $tBPMO = new Blood_PressureModel;
        $tBDatas = $tBPMO->field('id,high_pressure,low_pressure,high_status,low_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['1'] = empty($tBDatas)?array():$tBDatas;
        #血糖
        $tBSMO = new Blood_SugarModel;
        $tSDatas = $tBSMO->field('id,blood_sugar,status sugar_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['2'] = empty($tSDatas)?array():$tSDatas;
        #体温
        $tBTMO = new BodytemperatureModel;
        $tBTDatas = $tBTMO->field('id,temperature,status temperature_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['3'] = empty($tBTDatas)?array():$tBTDatas;
        #精神状态
        $tMMO = new MoodModel;
        $tMDatas = $tMMO->field('id,status mood_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        if(!empty($tMDatas)){
        foreach($tMDatas as $tKey=>$tValue){
            switch($tValue['mood_status']){
                case 1:$tMDatas[$tKey]['name'] = '开心';break;
                case 2:$tMDatas[$tKey]['name'] = '正常';break;
                case 3:$tMDatas[$tKey]['name'] = '焦虑';break;
                case 4:$tMDatas[$tKey]['name'] = '孤独';break;
                case 5:$tMDatas[$tKey]['name'] = '自卑';break;
                case 6:$tMDatas[$tKey]['name'] = '多疑';break;
                case 7:$tMDatas[$tKey]['name'] = '忧郁';break;
                case 8:$tMDatas[$tKey]['name'] = '恐惧';break;
            }
        }
        }
        $tDatas['4'] = empty($tMDatas)?array():$tMDatas;
        #皮肤状态
        $tSkMO = new SkinModel;
        $tSkDatas = $tSkMO->field('id,skin_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        if(!empty($tSkDatas)){
            foreach($tSkDatas as $tKey=>$tValue){
                switch($tValue['skin_status']){
                    case 1:$tSkDatas[$tKey]['name'] = '完好';break;
                    case 2:$tSkDatas[$tKey]['name'] = '轻度';break;
                    case 3:$tSkDatas[$tKey]['name'] = '中度';break;
                    case 4:$tSkDatas[$tKey]['name'] = '重度';break;
                }
            }
        }
        $tDatas['5'] = empty($tSkDatas)?array():$tSkDatas;
  
        #洗漱及护理
        $tNMO = new NursingModel;
        $tNDatas = $tNMO->field('id,type,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['6'] = empty($tNDatas)?array():$tNDatas;
        #胰岛素
        $tISMO = new InsulinModel;
        $tISDatas = $tISMO->field('*')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['7'] = empty($tISDatas)?array():$tISDatas;
        #用饭
        $tEMO = new EatingModel;
        $tEDatas = $tEMO->field('id,at,appetite,created')->where('sid = '.$pSid.' and '.$tWhere.' and type = 1')->fList();
        if(!empty($tEDatas)){
            foreach($tEDatas as $tKey=>$tValue){
                switch($tValue['at']){
                    case 1:$tEDatas[$tKey]['name'] = '早饭';break;    
                    case 3:$tEDatas[$tKey]['name'] = '午饭';break;    
                    case 5:$tEDatas[$tKey]['name'] = '晚饭';break;    
                }    
                switch($tValue['appetite']){
                    case 1:$tEDatas[$tKey]['a_name'] = '少量';
                    case 2:$tEDatas[$tKey]['a_name'] = '一般';
                    case 3:$tEDatas[$tKey]['a_name'] = '大量';
                }
            }
        }
        $tDatas['8'] = empty($tEDatas)?array():$tEDatas;
        #喝水
        $tDMO = new DrikingModel;
        $tDDatas = $tDMO->field('id,total num,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['9'] = empty($tDDatas)?array():$tDDatas;
        #用药
        $tMeMO = new MedicineModel;
        $tMLMO = new MedicinelibraryModel;
        $tSql = 'select ml.name name,m.num num,m.unit unit,m.created created from '.$tMeMO->table.' m left join '.$tMLMO->table.' ml on m.ml_id = ml.id where m.sid = '.$pSid.' and m.type = 1 and m.date = \''.date('Y-m-d',$tTime).'\'';
        $tMeDatas = $tMeMO->query($tSql);
        if(!empty($tMeDatas)){
        foreach($tMeDatas as $tK=>$tV){
            switch($tMeDatas[$tK]['unit']){
                case '1':$tMeDatas[$tK]['unit'] = '片';break;
                case '2':$tMeDatas[$tK]['unit'] = '支';break;
                case '3':$tMeDatas[$tK]['unit'] = '盒';break;
                case '4':$tMeDatas[$tK]['unit'] = '粒';break;
                case '5':$tMeDatas[$tK]['unit'] = '甁';break;
            }
        }
        }
        $tDatas['10'] = empty($tMeDatas)?array():$tMeDatas;
        #仪表
        $tAMO = new AppearanceModel;
        $tADatas = $tAMO->field('id,type,created')->where('sid = '.$pSid.' and '.$tWhere.' and type = 1')->fList();
        if(!empty($tADatas)){
            foreach($tADatas as $tKey=>$tValue){
                $tAData = explode(',',$tValue['type']);
                foreach($tAData as $tK=>$tV){
                    switch($tV){
                        case 1:$tAData[$tK] = '理发';
                        case 2:$tAData[$tK] = '刮脸';
                        case 3:$tAData[$tK] = '剪指甲';
                        case 4:$tAData[$tK] = '洗头';
                        case 5:$tAData[$tK] = '洗澡';
                        case 6:$tAData[$tK] = '洗脚';
                        case 7:$tAData[$tK] = '修脚';
                    }
                }
                $tAData = empty($tValue['type'])?'':implode(',',$tValue['type']);
            }
        }
        $tDatas['14'] = empty($tADatas)?'':$tADatas;
        #床铺管理
        $tBMMO = new BedmanagementModel;
        $tBMDatas = $tBMMO->field('id,pillow_status,pillowcovers_status,pillowcase_status,sheets_status,mattess_status,quilt_status,quiltcover_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['16'] = empty($tBMDatas)?array():$tBMDatas;
        #衣服换洗
        $tWMO = new WashclothModel;
        $tWDatas = $tWMO->field('id,underdress_status,coat_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['17'] = empty($tWDatas)?array():$tWDatas;
        #康复干预
        $tRIMO = new RehabilitationinterventionModel;
        $tRIDatas = $tRIMO->field('id,type,duration,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['15'] = empty($tRIDatas)?array():$tRIDatas;
        #探视
        $tVMO = new VisitModel;
        $tVDatas = $tVMO->field('id,relative_status,friend_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['18'] = empty($tVDatas)?array():$tVDatas;
        #睡眠
        $tSlMO = new SleepModel;
        $tSlDatas = $tSlMO->field('id,status,count,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        if(!empty($tSlDatas)){
        foreach($tSlDatas as $tK=>$tV){
            switch($tSlDatas[$tK]['status']){
                case '1':$tSlDatas[$tK]['status'] = '正常';break;
                case '2':$tSlDatas[$tK]['status'] = '暴动';break;
                case '3':$tSlDatas[$tK]['status'] = '无睡意';break;
            }
        }
        }
        $tDatas['21'] = empty($tSlDatas)?array():$tSlDatas;
        #加餐
        $tEjDatas = $tEMO->field('id,appetite,created')->where('sid = '.$pSid.' and type = 2 and '.$tWhere)->fList();
        if(!empty($tEjDatas)){
            foreach($tEjDatas as $tK=>$tV){
                switch($tV['appetite']){
                    case 1:$tEjDatas[$tK]['appetite'] = '少量';
                    case 2:$tEjDatas[$tK]['appetite'] = '一般';
                    case 3:$tEjDatas[$tK]['appetite'] = '大量';
                }
            } 
        }
        $tDatas['13'] = empty($tEjDatas)?array():$tEjDatas;
        #大便
        $tExMO = new ExcretionModel;
        $tExDatas = $tExMO->field('id,big_status,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['11'] = empty($tExDatas)?array():$tExDatas;
        $tExDatas1 = $tExMO->field('id,small_status,small_times,upd,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        $tDatas['12'] = empty($tExDatas1)?array():$tExDatas1;
        #午睡
        $tStMO = new SiestaModel;
        $tStDatas = $tStMO->field('id,duration,created')->where('sid = '.$pSid.' and '.$tWhere)->fList();
        if(!empty($tSlDatas)){
        foreach($tStDatas as $tK=>$tV){
            switch($tStDatas[$tK]['duration']){
                case '1':$tStDatas[$tK]['duration'] = '适中';break;
                case '2':$tStDatas[$tK]['duration'] = '较短';break;
                case '3':$tStDatas[$tK]['duration'] = '较长';break;
                case '4':$tStDatas[$tK]['duration'] = '无眠';break;
            }
        }
        }
        $tDatas['19'] = empty($tStDatas)?array():$tStDatas;
        #临睡用药
        $tSql = 'select ml.name name,m.num num,m.unit unit,m.created created from '.$tMeMO->table.' m left join '.$tMLMO->table.' ml on m.ml_id = ml.id where sid = '.$pSid.' and type = 2 and '.$tWhere;
        $tMelDatas = $tMeMO->query($tSql);
        if(!empty($tMelDatas)){
        foreach($tMelDatas as $tK=>$tV){
            switch($tMelDatas[$tK]['unit']){
                case '1':$tMelDatas[$tK]['unit'] = '片';break;
                case '2':$tMelDatas[$tK]['unit'] = '支';break;
                case '3':$tMelDatas[$tK]['unit'] = '盒';break;
                case '4':$tMelDatas[$tK]['unit'] = '粒';break;
                case '5':$tMelDatas[$tK]['unit'] = '甁';break;
            }
        }
        }
        $tDatas['22'] = empty($tMelDatas)?array():$tMelDatas;
        
        unset($tDatas[0]);
        foreach($tDatas as $tK=>$tV){
            if($tK != $pEventtype){
                $tDatas[$tK] = array();
            }
        }

        Tool_Fnc::ajaxMsg('',1,$tDatas);
    }
}
?>
